require("../less/personal.less")

document.ready(function () {
    let backBtn = document.querySelector('#backBtn')
    let showBirthPicker = document.querySelector('#showBirthPicker')
    let sexSelect = document.querySelector('#sexSelect')
    let birthSelect = document.querySelector('#birthSelect')
    let showProvincePicker = document.querySelector('#showProvincePicker')
    let showCityPicker = document.querySelector('#showCityPicker')
    let provinceSelect = document.querySelector('#provinceSelect')
    let citySelect = document.querySelector('#citySelect')
    backBtn.addEventListener('click', function (e) {
        history.back();
    })
    // 性别
    showSexPicker.addEventListener('click', function () {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            // disabled: true,
            value: 1
        }], {
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                console.log(result);
                sexSelect.innerHTML = result[0].label
            },
            title: '单列选择器'
        });
    });
    // 生日
    showBirthPicker.addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                // console.log(result);
                let birthStr = result.map(function (obj) {
                    return window.$util.addZero(obj.value);
                }).join("-")
                console.log(birthStr);
                birthSelect.textContent = birthStr
                

            },
            title: '多列选择器'
        });


    })
    // 省份
    showProvincePicker.addEventListener('click', function () {
        let localArr =JSON.parse(window.localStorage.getItem('provinceArr'))
        console.log(localArr);
        let provinceArr = localArr.map(function(obj){
            let newObj = {
                label:obj.name,
                value:obj.id
            }
            return newObj;
        })
        weui.picker(
            provinceArr, {
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                provinceArr.map(function(proObj){
                   console.log( proObj.name);
                })
                // 显示省份
                provinceSelect.innerHTML = result[0].label;
                // 获取省份
                //发请求获取管辖的城市
                axios.get("http://139.9.177.51:3701/api/shared/city?provinceId=" + result[0].value)
                .then(res => {
                    console.log(res);
                    
                })
                .catch(err => {
                    console.log(err);
                })


            },
            title: '请选择省份'
        });
        
    });
    // 市选择
    showCityPicker.addEventListener('click', function () {
        let localArr =JSON.parse(window.localStorage.getItem('provinceArr'))
        console.log(localArr);
        let provinceArr = localArr.map(function(obj){
            let newObj = {
                label:obj.name,
                value:obj.id
            }
            return newObj;
        })
        weui.picker(
            provinceArr, {
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                provinceArr.map(function(proObj){
                   console.log( proObj.name);
                })
                citySelect.innerHTML = result[0].label
            },
            title: '市选择器'
        });
        
    });

    // 市
})